package game;

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;

import common.Math3D;
import common.Point3D;
import common.Position3D;



public class TracingCamera extends Camera {
	Point3D target_t;
	Position3D source;
	Point3D eye;
	
	double up_distance = 5;
	double face_distance = 5;

	private int width;
	private int height;
	private Point3D up;

	public void setTarget(Position3D target) {
		this.target_t = target.getCenter();
		this.source = target;;
	}

	public void setTarget(Position3D target, Position3D source) {
		this.target_t = target.getCenter();
		this.source = source;
	}
	public void setTarget(Point3D vp, Position3D source) {
		this.target_t = vp;
		this.source = source;
	}
	
	public void setDistance(double distance) {
		this.face_distance = distance;
		this.up_distance = distance;
	}

	public void setUpDistance(double distance) {
		this.up_distance = distance;
	}
	public void setFaceDistance(double distance) {
		this.face_distance = distance;
	}
	public double getUpDistance(){
		return up_distance;
	}
	public double getFaceDistance() {
		return face_distance;
	}

	
	
	@Override
	public void paint(GL gl, int width, int height) {
	    this.width = width;
	    this.height = height;
		
	    updateView(gl);
	    	}
	@Override
	public void updateView(GL gl){
		final GLU glu = new GLU();

		gl.glPushMatrix();
	    gl.glViewport(0, 0, width, height);
	    gl.glMatrixMode(GL.GL_PROJECTION);
	    gl.glLoadIdentity();
	    
	    glu.gluPerspective(fovy, (double)width / (double)height, znear, zfar);

		position = source.getCenter().clone();
		position.sub(Math3D.scalePoint(source.getUp(), -up_distance));
		position.sub(Math3D.scalePoint(source.getFace(), face_distance));
		
		Point3D at = target_t;
		up = source.getFace().clone();
		glu.gluLookAt(position.x,position.y,position.z,at.x,at.y,at.z,up.x,up.y,up.z);
		gl.glMatrixMode(GL.GL_MODELVIEW);

	    gl.glPopMatrix();

	}
	
	@Override
	public Point3D getUp(){
		return up;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	

	
}
